#include "common.h"
#include "error.h"

namespace cpfs {

static VARATTR_THREAD CpfsErrno g_cpfs_errno;
static char const *CpfsErrnoStrings[] = {    
	"NoError",
	"BadFileDescriptor",
	"TooManyOpenFiles",
	"FilesystemFull",
	"FilesystemCorrupt",
	"FilenameTooLong",
	"InvalidPath",
	"PathTooLong",
	"PathNotFound",
	"DirectoryNotEmpty",
	"IsRoot",
	"FileExists",
	"IsDirectory",
	"NotDirectory",
	"NotSymlink",
};
LINKAGE("C") char const *cpfs_errno_str(CpfsErrno const e) {
    assert(CpfsErrnoStrings[e]);
    return CpfsErrnoStrings[e];
}    
void cpfs_errno_set(CpfsErrno const e) {
    if (e != 0 && cpfs_errno_isset()) {
        LOGF(Warning, "cpfs_errno = %s [%d] was %s [%d]",
            strerror(cpfs_errno_get()), cpfs_errno_get(), strerror(e), e);
    }
    g_cpfs_errno = e;
}
void cpfs_errno_clear() {
    g_cpfs_errno = NoError;
}
LINKAGE("C") bool cpfs_errno_isset() {
    return g_cpfs_errno != NoError;
}
LINKAGE("C") CpfsErrno cpfs_errno_get() {
    return g_cpfs_errno;
}

} // namespace cpfs {
